#
# @lc app=leetcode.cn id=859 lang=python3
#
# [859] 亲密字符串
#
"""
思路就是满足条件的，只有两种情况：
1）有且仅有两个位置的字母交换后相同
2）两个字符串本来就相同，但有一个位置的字母出现次数大于2
"""
# @lc code=start
class Solution:
    def buddyStrings(self, s: str, goal: str) -> bool:
        length1 = len(s)
        length2 = len(goal)
        if length1 != length2:
            return False
        s_index = -1
        goal_index = -1
        count_list = [0] * 26
        for i in range(length1):
            count_list[ord(s[i]) - ord('a')] += 1
            if s[i] != goal[i]:
                if s_index == -1:
                    s_index = i
                elif goal_index == -1:
                    goal_index = i
                else:
                    return False
        return (s_index != -1 and goal_index != -1 and s[s_index] == goal[goal_index] and s[goal_index] == goal[s_index]) or (s_index == -1 and goal_index == -1 and any(item > 1 for item in count_list))
                    
# @lc code=end

